import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:reader_app/page_card/car_share.dart';
import 'package:reader_app/page_card/card_recommend.dart';
import 'package:reader_app/page_card/card_test.dart';

import 'page_card/card_free.dart';
import 'page_card/card_special.dart';

class ContentPager extends StatefulWidget {
  final int _currentPage;
  final ValueChanged<int> _valueChanged;
  final ContentPagerController contentPagerController;

  ContentPager(this._currentPage, this._valueChanged, {Key key, this.contentPagerController}) : super(key: key);

  @override
  _ContentPagerState createState() => _ContentPagerState();
}

class _ContentPagerState extends State<ContentPager> {
  static List<Color> pageColors = [Colors.blue, Colors.red, Colors.deepOrange, Colors.teal];
  PageController _pageController;

  @override
  void initState() {
    _statusBar();
    _pageController = new PageController(viewportFraction: 0.9, initialPage: widget._currentPage);
    widget.contentPagerController?.pageController = _pageController;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new PageView(
        children: <Widget>[
          _warpItem(CardRecommend()),
          _warpItem(CardShare()),
          _warpItem(CardFree()),
          _warpItem(CardSpecial()),
          _warpItem(CardTest())
        ],
        onPageChanged: widget._valueChanged,
        controller: _pageController,
      ),
    );
  }

  Widget _warpItem(Widget widget) {
    return new Padding(
      padding: EdgeInsets.all(5),
      child: new Container(
        child: widget,
      ),
    );
  }

  _statusBar() {
    SystemUiOverlayStyle _uiStyle = new SystemUiOverlayStyle(
      systemNavigationBarColor: Color(0xFF000000),
      systemNavigationBarDividerColor: null,
      statusBarColor: Colors.transparent,
      systemNavigationBarIconBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness: Brightness.light,
    );
    SystemChrome.setSystemUIOverlayStyle(_uiStyle);
  }
}

///内容区域控制器
class ContentPagerController {
  PageController pageController;

  void jump2Page(int page) {
    pageController?.jumpToPage(page);
  }
}
